﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace SqliteLoader
{
	public partial class ProgressDialog : Form
	{
		public ProgressDialog()
		{
			InitializeComponent();
		}

		private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
		{
			Debug.Assert(e.ProgressPercentage <= 100);
			if(e.ProgressPercentage >= 0 && e.ProgressPercentage <= 100)
				progressBar.Value = e.ProgressPercentage;

			var message = e.UserState as string;
			if(message != null)
				AddMessage(message);
			else
			{
				var state = e.UserState as ProgressState;
				if (state != null)
				{
					if (state.Message != null)
						AddMessage(state.Message);
					if (state.Status != null)
						statusLabel.Text = state.Status;
				}

			}

		}

		public void AddMessage(string message)
		{
			messageList.SelectedIndex = messageList.Items.Add(message);
			statusLabel.Text = string.Empty;
		}
		
		private void CancelButton_Click(object sender, EventArgs e)
		{
			backgroundWorker.CancelAsync();
			cancel.Enabled = false;
		}

		private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
		{
			cancel.Visible = false;
			CloseButton.Visible = true;
			progressBar.Visible = false;
		}

		private void CloseButton_Click(object sender, EventArgs e)
		{
			Close();
		}
	}
}
